package com.mbiolance.cloud.activiti.restful;

import com.mbiolance.cloud.activiti.domain.dto.ModelDto;
import com.mbiolance.cloud.activiti.domain.dto.ProcessDefinitionDto;
import com.mbiolance.cloud.activiti.rpc.ProcessDefinitionFeignService;
import com.mbiolance.cloud.activiti.service.ProcessDefinitionService;
import com.mbiolance.cloud.auth.domain.PageModel;
import com.mbiolance.cloud.auth.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("process-definition")
public class ProcessDefinitionRestful implements ProcessDefinitionFeignService {

    @Autowired
    private ProcessDefinitionService processDefinitionService;

    @Override
    public R<List<ProcessDefinitionDto>> list() {
        List<ProcessDefinitionDto> list = processDefinitionService.list();
        return R.ok(list);
    }

    public R<PageModel<ProcessDefinitionDto>> page(Integer number, Integer size) {
        PageModel<ProcessDefinitionDto> page = processDefinitionService.page(number, size);
        return R.ok(page);
    }

    public R<Boolean> deploy(String modelId) {
        Boolean deploy = null;
        try {
            deploy = processDefinitionService.deploy(modelId);
        } catch (Exception e) {
            log.error("ProcessDefinitionRestful deploy error: {}", e.getMessage());
            return R.ok(false);
        }
        return R.ok(deploy);
    }

    public R<PageModel<ModelDto>> modelPage(Integer number, Integer size) {
        PageModel<ModelDto> page = processDefinitionService.modelPage(number, size);
        return R.ok(page);
    }

    public R<Boolean> deleteModel(String modelId) {
        Boolean deleted = processDefinitionService.deleteModel(modelId);
        return R.ok(deleted);
    }

}
